//
//  AY_BookMalePage.m
//  AiYue
//
//  Created by boWen on 2023/10/16.
//

#import "AY_BookManPage.h"
#import "AY_BookCell0.h"
#import "AY_BookCell1.h"
#import "AY_BookCell2.h"
#import "AY_BookCell3.h"
#import "AY_BookBDCell.h"
@interface AY_BookManPage ()<UITableViewDelegate, UITableViewDataSource,SXLoadingDelegate>
@property (nonatomic, strong) UITableView * baseTableView;
@property (nonatomic, strong) NSMutableArray * dataAry;
@property (strong,nonatomic) AY_LoadingView *loadingView;
@end

@implementation AY_BookManPage

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initWithBaseUI];
    [self getConfigData];
}

- (void)initWithBaseUI {
    self.gk_navigationBar.hidden = YES;
    [self.view addSubview:self.baseTableView];
}


- (void)getConfigData {
    [self.loadingView loadingInView:self.view];
    [AYApiRequest getMallChannelControlWithChannelId:self.channelId success:^(AY_ChannelModel * _Nonnull pkg) {
        if([pkg.code isEqualToString:@"200"]) {
            [self.loadingView dismiss];
            self.dataAry = [NSMutableArray arrayWithArray:pkg.data];
            [self.baseTableView reloadData];
        }else {
            [self.loadingView refresh];
        }
    } failure:^(int status, NSString * _Nonnull info) {
        [self.loadingView refresh];
    }];
}


- (UITableView *)baseTableView {
    if (!_baseTableView) {
        UITableView * myTableView = [AYTool creatMyTableView:CGRectMake(0, 0, GK_SCREEN_WIDTH, GK_SCREEN_HEIGHT  - GK_TABBAR_HEIGHT - GK_STATUSBAR_HEIGHT - GK_SAFEAREA_BTM - YNUM(10))];
        myTableView.delegate = self;
        myTableView.dataSource = self;
        [myTableView registerClass:[AY_BookCell0 class] forCellReuseIdentifier:@"AY_BookCell0"];
        [myTableView registerClass:[AY_BookCell1 class] forCellReuseIdentifier:@"AY_BookCell1"];
        [myTableView registerClass:[AY_BookCell2 class] forCellReuseIdentifier:@"AY_BookCell2"];
        [myTableView registerClass:[AY_BookCell3 class] forCellReuseIdentifier:@"AY_BookCell3"];
        [myTableView registerClass:[AY_BookBDCell class] forCellReuseIdentifier:@"AY_BookBDCell"];
        _baseTableView = myTableView;
    }
    return _baseTableView;
}

- (NSMutableArray *)dataAry {
    if(!_dataAry) {
        _dataAry = [@[] mutableCopy];
    }
    return _dataAry;
}

#pragma mark - UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.dataAry.count > indexPath.section) {
        ChannelListModel * model = self.dataAry[indexPath.section];
        if ([model.displayStyle.styleCode isEqualToString:@"banner"]) {
            AY_BookCell0 *cell = [tableView dequeueReusableCellWithIdentifier:@"AY_BookCell0"];
            cell.model = model;
            return cell;
        }
        else if([model.displayStyle.styleCode isEqualToString:@"mrjx"]) {
            AY_BookCell2 *cell = [tableView dequeueReusableCellWithIdentifier:@"AY_BookCell2"];
            cell.model = model;
            return cell;
        }else if([model.displayStyle.styleCode isEqualToString:@"vertical"]) {
            AY_BookCell3 *cell = [tableView dequeueReusableCellWithIdentifier:@"AY_BookCell3"];
            cell.model = model;
            return cell;
        }else if([model.displayStyle.styleCode isEqualToString:@"inline-4"]) {
            AY_BookCell1 *cell = [tableView dequeueReusableCellWithIdentifier:@"AY_BookCell1"];
            cell.model = model;
            return cell;
        }
        else if([model.displayStyle.styleCode isEqualToString:@"bd"]) {
            AY_BookBDCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AY_BookBDCell"];
            cell.model = model;
            return cell;
        }
        else {
            UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
            if (!cell) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell1"];
            }
            return cell;
        }
    }else {
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell3"];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell3"];
        }
        return cell;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataAry.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.dataAry.count > indexPath.section) {
        ChannelListModel * model = self.dataAry[indexPath.section];
        if ([model.displayStyle.styleCode isEqualToString:@"banner"]) {
            return YNUM(125);
        }else if([model.displayStyle.styleCode isEqualToString:@"mrjx"]) {
            return YNUM(223);
        }else if([model.displayStyle.styleCode isEqualToString:@"vertical"]) {
            return model.items.count *  YNUM(92) + model.items.count*YNUM(10) + YNUM(75);//(62为其他空间高度 + 142为cell的行高)
        }else if([model.displayStyle.styleCode isEqualToString:@"inline-4"]) {
            return model.items.count <= 4 ? YNUM(224) : YNUM(360);
        }else if([model.displayStyle.styleCode isEqualToString:@"bd"]) {
            return YNUM(390);
        }else {
            return 0;
        }
    }else {
        return 0;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return YNUM(10);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView * view = [[UIView alloc] init];
    view.frame = CGRectMake(0, 0, GK_SCREEN_WIDTH, YNUM(10));
    return view;
}

- (AY_LoadingView *)loadingView {
    if (!_loadingView) {
        _loadingView = [[AY_LoadingView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H - GK_TABBAR_HEIGHT)];
        _loadingView.backgroundColor = COLOR(@"#F7F5F6");
        _loadingView.delegate = self;
        _loadingView.hideLeftpop = YES;
    }
    return _loadingView;
}
@end
